package com.smart.campus.school.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.school.domain.ExamRoomClasses;
import com.smart.campus.school.domain.ExamRoomSchedule;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.mapper.ExamRoomClassesMapper;
import com.smart.campus.school.mapper.ExamRoomScheduleMapper;
import com.smart.campus.school.mapper.StudentMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.ExamRoomStudentsMapper;
import com.smart.campus.school.domain.ExamRoomStudents;
import com.smart.campus.school.service.IExamRoomStudentsService;
import com.smart.campus.common.core.text.Convert;

/**
 * 考场学生安排Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-06-06
 */
@Service
public class ExamRoomStudentsServiceImpl implements IExamRoomStudentsService 
{
    @Autowired
    private ExamRoomStudentsMapper examRoomStudentsMapper;

    @Autowired
    private ExamRoomScheduleMapper examRoomScheduleMapper;

    @Autowired
    private ExamRoomClassesMapper examRoomClassesMapper;

    @Autowired
    private StudentMapper studentMapper;
    /**
     * 查询考场学生安排
     * 
     * @param id 考场学生安排ID
     * @return 考场学生安排
     */
    @Override
    public ExamRoomStudents selectExamRoomStudentsById(Long id)
    {
        return examRoomStudentsMapper.selectExamRoomStudentsById(id);
    }

    /**
     * 查询考场学生安排列表
     * 
     * @param examRoomStudents 考场学生安排
     * @return 考场学生安排
     */
    @Override
    public List<ExamRoomStudents> selectExamRoomStudentsList(ExamRoomStudents examRoomStudents)
    {
        return examRoomStudentsMapper.selectExamRoomStudentsList(examRoomStudents);
    }

    /**
     * 新增考场学生安排
     * 
     * @param examRoomStudents 考场学生安排
     * @return 结果
     */
    @Override
    public int insertExamRoomStudents(ExamRoomStudents examRoomStudents)
    {
        return examRoomStudentsMapper.insertExamRoomStudents(examRoomStudents);
    }

    /**
     * 修改考场学生安排
     * 
     * @param examRoomStudents 考场学生安排
     * @return 结果
     */
    @Override
    public int updateExamRoomStudents(ExamRoomStudents examRoomStudents)
    {
        return examRoomStudentsMapper.updateExamRoomStudents(examRoomStudents);
    }

    /**
     * 删除考场学生安排对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteExamRoomStudentsByIds(String ids)
    {
        return examRoomStudentsMapper.deleteExamRoomStudentsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除考场学生安排信息
     * 
     * @param id 考场学生安排ID
     * @return 结果
     */
    @Override
    public int deleteExamRoomStudentsById(Long id)
    {
        return examRoomStudentsMapper.deleteExamRoomStudentsById(id);
    }

    @Override
    public void batchInsertInfo(@Param("list") List<ExamRoomStudents> list) {
        examRoomStudentsMapper.batchInsertInfo(list);
    }

    /**
     * 根据学号分配考场
     * @return
     */
    @Override
    public AjaxResult distributionRoomByNO(Long scheduleId) {
        AjaxResult result = null;
        //获取年级考试安排
        ExamRoomSchedule schedule = examRoomScheduleMapper.selectExamRoomScheduleById(scheduleId);
        if(schedule !=null){//是否有安排
            ExamRoomClasses examRoomClasses = new ExamRoomClasses();
            examRoomClasses.setScheduleId(scheduleId);
            //根据考试安排Id获取考场列表
            List<ExamRoomClasses> roomList = examRoomClassesMapper.selectExamRoomClassesList(examRoomClasses);
            if(CollectionUtils.isNotEmpty(roomList)){//有获取到考场数据
                //查询年级学生记录
                Student student = new Student();
                student.setGradeId(schedule.getGradeId());//设置年级Id
                List<Student> list = studentMapper.selectStudentListOrderByStudentNo(student);
                if(CollectionUtils.isNotEmpty(list)){//有学生数据
                    int total = schedule.getTotal();
                    int studentsCount = list.size();
                    if(total<studentsCount){
                        result = AjaxResult.warn("参加考试的人数："+studentsCount+",考场安排总数："+total);
                    }else{
                        int roomCount = 0;
                        int gradeOrderNum = 0;//年级排名
                        for (ExamRoomClasses roomClasses: roomList ) {
                            if(roomCount>list.size()){
                                break;
                            }
                            int roomTotal = roomClasses.getNumbers();
                            List<Student> roomStudentsList =  list.subList(roomCount,roomTotal>list.size()?list.size():roomTotal);
                            List<ExamRoomStudents> examRoomStudentsList = new ArrayList<>();
                            int roomOrderNum = 0;//考场排名
                            for(Student student1 : roomStudentsList){
                                ExamRoomStudents examRoomStudents = new ExamRoomStudents();
                                examRoomStudents.setClazzId(roomClasses.getId());
                                examRoomStudents.setStudentId(student1.getId());
                                examRoomStudents.setScheduleId(scheduleId);
                                int rod = roomOrderNum+1;
                                examRoomStudents.setOrderGrade(gradeOrderNum+1);
                                examRoomStudents.setOrderClazz(roomOrderNum+1);
                                String siteNo = rod>9?rod+"":"0"+rod;
                                examRoomStudents.setSiteNo(siteNo);
                                roomOrderNum++;
                                gradeOrderNum++;
                                examRoomStudentsList.add(examRoomStudents);
//                                examRoomStudentsMapper.insertExamRoomStudents(examRoomStudents);
                            }
                            if(examRoomStudentsList.size()>0)
                                examRoomStudentsMapper.batchInsertInfo(examRoomStudentsList);
                            roomCount += roomTotal;
                            if(roomCount>list.size()){
                                break;
                            }
                        }
                        result = AjaxResult.success("生成成功！");
                    }

                }else{
                    result = AjaxResult.warn("该年级还没有学生!!!");
                }
            }else{
                result = AjaxResult.warn("还没有安排考场!!!");
            }
        }else{
            result = AjaxResult.warn("没有查询到考试安排!!!");
        }
        return result;
    }

    @Override
    public int checkScheduleStudents(Long scheduleId) {
        return examRoomStudentsMapper.checkScheduleStudents(scheduleId);
    }
}
